﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Election.ToTheoDoiKetQua
{
    public partial class ThongKeUngCuVienForm : Form
    {
        public ThongKeUngCuVienForm()
        {
            InitializeComponent();
        }

        private void ThongKeUngCuVienForm_Load(object sender, EventArgs e)
        {
            try
            {
                //lay thong tin dot bau cu
                string query = "select MADOTBAUCU from quanlybaucu.dotbaucu";
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                if (dataTbl.Rows.Count == 0)
                {
                    MessageBox.Show("Không có thông tin đợt bầu cử nào trong hệ thống");
                    return;
                }
                for (int i = 0; i < dataTbl.Rows.Count; i++)
                {
                    this.dotBauCuCmbBox.Items.Add(dataTbl.Rows[i]["MADOTBAUCU"].ToString());
                }
                this.dotBauCuCmbBox.SelectedIndex = 0;
                loadDotBauCu(dataTbl.Rows[0]["MADOTBAUCU"].ToString());
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        public void loadDotBauCu(String maDotBauCu)
        {
            try
            {
                //CHECK HERE - thieu hinh anh
                //get info of ungcuvien
                string query = "select * from table(quanlybaucu.func_thongKeKetQuaBB('" + maDotBauCu + "')) ";
                DataTable dataTbl = orcl.ExecuteQueryThrowEx(query);
                UngCuVienUC[] ucArr = new UngCuVienUC[] { this.ungCuVienUC1, this.ungCuVienUC2, this.ungCuVienUC3, this.ungCuVienUC4, this.ungCuVienUC5 };
                //check if info is available
                if (dataTbl.Rows.Count == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        ucArr[i].setUngCuVienInfo("UNAVAILABLE", "", 0, "");
                    }
                    MessageBox.Show("Thông tin đợt bầu cử này chưa có trong hệ thống");
                    return;
                }

                // set each ucv info for ucv user control                
                for (int i = 0; i < 5; i++)
                {
                    if(i < dataTbl.Rows.Count)
                        ucArr[i].setUngCuVienInfo(dataTbl.Rows[i]["MAUCV"].ToString(), dataTbl.Rows[i]["TEN"].ToString(), int.Parse(dataTbl.Rows[i]["SOLUONG"].ToString()), dataTbl.Rows[i]["MAUCV"].ToString());
                    else
                        ucArr[i].setUngCuVienInfo("UNAVAILABLE", "", 0, "");
                }
                    
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dotBauCuCmbBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string maDotBauCu = this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString();
            loadDotBauCu(maDotBauCu);
          
        }

        private void refreshBut_Click(object sender, EventArgs e)
        {
            string maDotBauCu = this.dotBauCuCmbBox.Items[this.dotBauCuCmbBox.SelectedIndex].ToString();
            loadDotBauCu(maDotBauCu);
        }
       
    }
}
